/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.jwtools.tmt.ui.binder;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JComboBox;
import javax.swing.JComponent;

import net.jwtools.tmt.domain.Person;
import net.jwtools.tmt.service.PersonService;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.binding.form.FormModel;
import org.springframework.binding.value.ValueModel;
import org.springframework.richclient.form.binding.Binding;
import org.springframework.richclient.form.binding.support.AbstractBinder;
import org.springframework.richclient.form.binding.swing.ComboBoxBinding;
import org.springframework.util.Assert;

public class PersonBinder extends AbstractBinder implements InitializingBean {

	private PersonService personService;

	private Map<String, Person> persons;

	public PersonBinder() {
		super(Person.class, new String[] {});
	}

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(personService, "personService must be provided.");
		persons = new LinkedHashMap<String, Person>();
		List<Person> ps = personService.getAllActivePersons();
		for (Person person : ps) {
			persons.put(person.getDisplayName(), person);
		}
	}

	protected JComponent createControl(Map context) {
		return getComponentFactory().createComboBox();
	}

	protected Binding doBind(JComponent control, FormModel formModel,
			String formPropertyPath, Map context) {
		Assert.isTrue(control instanceof JComboBox, formPropertyPath);
		ComboBoxBinding binding = new ComboBoxBinding((JComboBox) control,
				formModel, formPropertyPath) {
			protected ValueModel getValueModel() {
				return super.getValueModel();
			}
		};
		binding.setSelectableItems(persons.keySet());
		return binding;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

//	// This is a hack to get the combo box working even though
//	// PetType does not implement equals/hashCode.
//	private class PetTypeAdapter extends TypeConverter {
//		private PetTypeAdapter(ValueModel valueModel, final Map petTypes) {
//			super(valueModel, new Closure() {
//				public Object call(Object petType) {
//					return petType != null ? ((PetType) petType).getName() : "";
//				}
//			}, new Closure() {
//				public Object call(Object petTypeName) {
//					return petTypes.get(petTypeName);
//				}
//			});
//		}
//	}
}